<?php
/**
 * Created by PhpStorm.
 * User: lenovo
 * Date: 2019/2/23
 * Time: 9:34
 */
namespace App\Services\Campaign\Platform;

use App\Model\Comments;
use App\Model\Enterprise;
use App\Model\Expertes;
use App\Model\Image;
use App\Model\Infomation;
use App\Model\ReUser;
use App\Model\User;
use App\Services\CommonService;
use function PHPSTORM_META\type;

//评论的控制器的service层
class CommentService extends CommonService
{

    /**
     * 添加入库   添加视频的评论
     *
     */
    public function CommentAdd($request)
    {
        $user_id = $request['user_id'];
        $comment_details = $request['comment_details'];
        $comment_type = $request['comment_type'];
        $comment_relate = $request['comment_relate'];
        $comment_img = $request['comment_img'];

        //查询用户id是否存在
        $uid = $this->ReUserId($user_id);
//        用户id不存在
        if ($uid == array()) {
            return 106;
        }

        switch ($comment_type) {
            //资讯
            case 1:
                {

                    //查看资讯内容是否存在
                    $information = $this->Infomation($comment_relate);
                    if ($information == array()) {
                        return 110;
                    }

                }
                break;
            //视频
            case 2:
                {
                    //查询专家是否存在
                    $relas = $this->Relation($comment_relate);

                    if (empty($relas) == true) {
                        return 107;
                    }
                    //上传视频


                }
                break;

            //企业评论
            case 3:
                {
                    $position_name = $request['position_name'];
                    $position_money = $request['position_money'];

                    //查看  评论的 公司是否存在
                    $company = $this->Company($comment_relate);
                    if (empty($company) == true) {
                        return 109;
                    }
                }
                break;
        }
        $use = new Comments();
        $use->user_id = $user_id;
        $use->comment_details = $comment_details;
        $use->comment_type = $comment_type;
        $use->comment_relate = $comment_relate;
        $use->comment_img = $comment_img;
        $use->timestamps = true;
        if ($comment_type == '3') {
            $use->position_name = $position_name;
            $use->position_money = $position_money;
        }
        $bool = $use->save();
        if ($bool != true) {
            return 103;
        } else {
            return 200;
        }
    }


    /**
     * 公司是否存在
     */
    public function Company($company_id)
    {
        $use = new Enterprise();
        $data = $use->find($company_id);
        if ($data) {
            return $data;
        } else {
            return array();
        }
    }


    //查询用户id是否存在
    public function ReUserId($user_id)
    {
        $data = ReUser::find($user_id);
        if ($data) {
            return $data;
        } else {
            return array();
        }
    }

    /**
     * 查找关联id是否存在   要评论的哪条数据
     * 专家
     */
    public function Relation($comment_relate)
    {
        $data = Expertes::where('id', '=', $comment_relate)->get()->toArray();
        if ($data) {
            return $data;
        } else {
            return array();
        }
    }


    /**
     * 查看资讯是否存在
     */
    public function Infomation($id)
    {
        $data = Infomation::find($id);
        if ($data) {
            return $data;
        } else {
            return array();
        }
    }


    /**
     * @param $request
     * 通过  评论类型和关联id 查出评论
     * 专家视频的查询
     */
    public function CommentSel($request)
    {
        //关于专家 视频的评论  查看专家的id是否存在
        $comment_relate = $request['comment_relate'];
        //验证评论的类型  关联的id是否存在
        switch ($request['comment_type']) {
            //资讯
            case 1:
                {
                    //查看资讯内容是否存在
                    $information = $this->Infomation($comment_relate);
                    if ($information == array()) {
                        return 110;
                    }
                }
                break;
            case 2:
                {
                    $res = $this->Relation($comment_relate);
                    //专家类型不存在
                    if ($res === array()) {
                        $res['code'] = 107;
                        return $res;
                    }
                }
                break;
//           企业评价
            case 3:
                {
                    //查看公司id是否存在
                    $company = $this->Company($comment_relate);
                    if ($company === array()) {
                        $res['code'] = 109;
                        return $res;
                    }
                }
                break;
        }
        //去查找  评价
        $data = Comments::where(['comment_type' => $request['comment_type'], 'comment_relate' => $comment_relate])->limit(5)->get()->toArray();

//        return $data;
//        return $data;
        if ($data) {

            //循环拿出专家评论  里的用户信息
            if ($request['comment_type'] == '2') {
                //拿出  user_id  并去重
                $u_id = array_column($data, 'user_id');
                $u_id = array_unique($u_id);
//                return $u_id;
                //去数据飙中查找  user 信息
                $useinfo = User::whereIn('id', $u_id)
                    ->get(['id', 'u_name', 'u_head_picture'])
                    ->toArray();

//                return $useinfo;
                //循环放入
                foreach ($data as &$v) {
                    foreach ($useinfo as $kk => $vv) {
                        if ($v['user_id'] == $vv['id']) {
                            $v['u_name'] = $vv['u_name'];
                            $v['header_img'] = $vv['u_head_picture'];
                        }
//                    }
                    }
                }

//                return $data;
            } //                拿出公司图片
            else if ($request['comment_type'] == '3') {
                //查出图片  去图片库里查  联查
                $imgs = Enterprise::where('id', '=', $comment_relate)
                    ->get()->first();
                //拿出图片id

                if($imgs){
                    //拿到 字段
                    $enterprise_photo=$imgs['enterprise_photo'];
                    //拼成数组
                    $enterprise_photo=explode(',',$enterprise_photo);
                    //查找图片
                    $photo=Image::whereIn('id',$enterprise_photo)->get(['id','img_url'])->toArray();
                    //放到数组中
                    if($photo){
                        foreach ($photo as &$item) {
                            $img[]=$item['img_url'];
                        }
                    }else{
                        $arr['code'] = 111;
                        return $arr;
                    }
                }else{
                    //没查到图片
                    $arr['code'] = 111;
                    return $arr;
                }
                $arr['img']=$img;
            }
                $arr['code'] = 200;
                $arr['data'] = $data;
                return $arr;
            } else {
                $arr['code'] = 112;
                return $arr;
            }

        }


}